package com.baizhi.action;

import com.baizhi.entity.Person;
import com.baizhi.service.PersonService;
import com.baizhi.service.impl.PersonServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PersonAction {
    private PersonService ps = new PersonServiceImpl();
    private Person p;
    private Integer pageNum = 1;
    private Integer pageSize = 8;
    private Integer totalPage;
    private List<Person> list;
    private Map<String,Object> map = new HashMap<>();
    public Person getP() {
        return p;
    }

    public void setP(Person p) {
        this.p = p;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<Person> getList() {
        return list;
    }

    public void setList(List<Person> list) {
        this.list = list;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public String add(){
        int i = ps.addPerson(p);
        map.put("code",i);
        return "addOK";
    }
    public String remove(){
        System.out.println(p.getId());
        int i = ps.deletePerson(p.getId());
        map.put("code",i);
        return "removeOK";
    }
    public String select(){
        System.out.println(p);
        String name = null;
        if(p!=null){
            name = p.getName();
        }
        list = ps.selectPerson(name,pageNum,pageSize);
        int count = ps.count(name);
        totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;
        map.put("list",list);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("totalPage",totalPage);
        return "selectOK";
    }
}
